package org.bhf.service;

import org.bhf.util.AnnotationAccess;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

/**
 * Declare the resource, such as a JSP, responsible for rendering this widget in conjunction with a
 * <code>PageProcessView</code>.
 */
@Documented
@Retention( RetentionPolicy.RUNTIME  )
@Target( { ElementType.TYPE, ElementType.METHOD } )
@Inherited
public @interface View
{
    /**
     * Utility accessors for <code>Validate</code>
     * @exclude
     */
    public static final class Util
    {
        static final Class<View> clazz = View.class;

        /**
         * Return the URI to the view of this widget
         * @param serviceClass Widget.
         * @param method Target service method
         * @return Return the URI to the view of this widget
         */
        public static String view( final Class<?> serviceClass, final Method method )
        {
            final View      cView   = AnnotationAccess.getAnnotation( serviceClass, View.Util.clazz );
            final View      mView   = AnnotationAccess.getAnnotation( method, View.Util.clazz );
                  String    view    = null;

            if( cView != null )
                view = cView.value();
            if( mView != null )
                view = mView.value();

            if( view == null )
                throw new IllegalArgumentException(
                    serviceClass.getName() + " or the method " + method.getName() + " must be annotated with a View." );

            return view;
        }
    }

    /**
     * The resource responsible for rendering this widget.
     * @return The resource responsible for rendering this widget.
     */
    String  value();
}
